﻿using SeeSharpTools.JY.DSP.Utility.Filter1D;
using Sunny.UI;
using System;
using System.Windows.Forms;

namespace SeeSharpTools.Demo
{
    public partial class FormFIRPM : UIPage
    {
        public FormFIRPM()
        {
            InitializeComponent();

            uiComboBox_FilterType.Items.AddRange(Enum.GetNames(typeof(PMFilterType)));
            uiComboBox_FilterType.SelectedIndex = 0;
        }

        private void uiButton_Design_Click(object sender, EventArgs e)
        {
            try
            {
                easyChartX_Mag.Miscellaneous.CheckInfinity = true;
                easyChartX_Phase.Miscellaneous.CheckInfinity = true;
                easyChartX_GrpDelay.Miscellaneous.CheckInfinity = true;
                easyChartX_Impulse.Miscellaneous.CheckInfinity = true;

                double[] bands = Array.ConvertAll(uiTextBox_Bands.Text.Split(','), Convert.ToDouble);
                double[] amplitudes = Array.ConvertAll(uiTextBox_Amplitudes.Text.Split(','), Convert.ToDouble);
                double[] weights = Array.ConvertAll(uiTextBox_Weights.Text.Split(','), Convert.ToDouble);
                double sampleRate = uiDoubleUpDown_SampleRate.Value;
                double[] b = FIRDesign.ParksMcClellan(uiIntegerUpDown_Order.Value, bands, amplitudes, weights,
                    (PMFilterType)Enum.Parse(typeof(PMFilterType), uiComboBox_FilterType.Text), sampleRate);
                var (mag, phase, f) = FIRAnalysis.Bode(b, sampleRate);
                easyChartX_Mag.Plot(f, mag);
                easyChartX_Phase.Plot(f, phase);
                var (gd, _) = FIRAnalysis.GroupDelay(b, sampleRate);
                easyChartX_GrpDelay.Plot(f, gd);
                var (h, t) = FIRAnalysis.ImpulseResponse(b, sampleRate);
                easyChartX_Impulse.Plot(t, h);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}